﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TeamOPSchoolCup.Domain.POCO;
using TeamOPSchoolCup.Domain.DAL;
using TeamOPSchoolCup.Domain.POCO.Members;
using TeamOPSchoolCup.Domain.Services;

namespace TeamOPSchoolCup.Web.Controllers
{
  public class TeamController : Controller
  {

    public ViewResult Profile(int id)
    {
      return View(TeamService.GetTeam(id));
    }

    public ViewResult Dashboard(int id)
    {
      //TODO
      //  Some meetings stuff
      //  Some conversations stuff
      //  Some bestResults stuff
      //  Some ranking stuff
      return View();
    }

    //
    // GET: /Team/Create

    public ActionResult Create()
    {
      ViewBag.CoachId = new SelectList(MemberService.GetCoachesAsMember(), "CoachId", "Member.Name");
      ViewBag.SportId = new SelectList(SportService.GetSports(true), "SportId", "Name");

      return View();
    }

    //
    // POST: /Team/Create

    [HttpPost]
    public ActionResult Create(Team team)
    {
      if (ModelState.IsValid)
      {
        TeamService.AddTeam(team);
        return RedirectToAction("Dasboard");
      }

      ViewBag.CoachId = new SelectList(MemberService.GetCoachesAsMember(), "CoachId", "Member.Name", team.CoachId);
      return View(team);
    }

    //
    // GET: /Team/Edit/5

    public ActionResult Edit(int id)
    {
      Team team = TeamService.GetTeam(id);
      ViewBag.CoachId = new SelectList(MemberService.GetCoachesAsMember(), "CoachId", "Member.Name", team.CoachId);
      ViewBag.SportId = new SelectList(SportService.GetSports(true), "SportId", "Name", team.SportId);
      
      return View(team);
    }

    //
    // POST: /Team/Edit/5

    [HttpPost]
    public ActionResult Edit(Team team)
    {
      if (ModelState.IsValid)
      {
        TeamService.UpdateTeam(team);
        return RedirectToAction("Index");
      }
      ViewBag.CoachId = new SelectList(MemberService.GetCoachesAsMember(), "CoachId", "Member.Name", team.CoachId);
      ViewBag.SportId = new SelectList(SportService.GetSports(true), "SportId", "Name", team.SportId);

      return View(team);
    }

    public ActionResult AddTeamMembership(int teamId)
    {
      //TODO Get all students within the school
      List<Student> students = MemberService.GetStudents();
      ViewBag.Students = students;
      return View();
    }

    [HttpPost]
    public ActionResult AddTeamMembership(int teamId, int studentId)
    {
      if (TeamService.AddTeamMembership(teamId, studentId))
      {
        return RedirectToAction("Details", new { id = teamId });
      }
      else
      {
        //TODO Errorpage
        return null;
      }
    }
  }
}